/*
 * @Author: zhoujinlong 765637726@qq.com
 * @Date: 2024-04-26 17:11:57
 * @LastEditors: zhoujinlong 765637726@qq.com
 * @LastEditTime: 2024-05-06 09:06:30
 * @FilePath: \election-yd\src\main\mysql\pool.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mysql = require('mysql')

// 创建连接池
const pool = mysql.createPool({
  host: 'localhost',
  port: '3306', // 端口号
  user: 'root', // 用户名
  password: 'root', // 密码
  database: 'long_test', // 数据库名
  connectionLimit: 10 // 控制连接池的大小
})

// 从连接池中获取连接并执行查询
pool.getConnection((err, connection) => {
  if (err) {
    console.error('Error getting MySQL connection:', err)
    return
  } else {
    // console.log('mysql success', 'err--',err,'connection---',connection)
    console.log('mysql success')
  }

  // // 示例查询
  connection.query('SELECT * FROM testtable', (err, results) => {
    if (err) {
      console.error('Error executing MySQL query:', err)
      return
    }

    console.log('Query results:', results)

    // 释放连接
    connection.release()
  })
})

export default pool
